package com.test.diaocha;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.inputmethodservice.InputMethodService;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.codetroopers.betterpickers.calendardatepicker.CalendarDatePickerDialogFragment;

import java.util.Calendar;
import java.util.TimeZone;

public class MainActivity extends AppCompatActivity {
    private static final String[] departments = new String[] {
        "计算机工程系",
        "电气与工程系",
        "经济与管理系",
        "传播与艺术系",
        "机械工程系",
        "环境与资源系"
    };

    EditText nameTxt;

    TextView dateTxt;
    long selectedDate = -1;

    CheckBox maleBtn;
    CheckBox femaleBtn;
    boolean isMale = true;

    Spinner departmentSpinner;
    String department;

    EditText phoneTxt;

    CheckBox yesBtn;
    CheckBox noBtn;
    int yesOrNo = -1;

    EditText adviseTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View focus = getCurrentFocus();
                if(focus != null) {
                    InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    focus.clearFocus();
                }
            }
        });

        nameTxt = findViewById(R.id.nameTxt);
        dateTxt = findViewById(R.id.dateTxt);
        dateTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
                if(selectedDate != -1){
                    calendar.setTimeInMillis(selectedDate);
                }
                CalendarDatePickerDialogFragment cdp = new CalendarDatePickerDialogFragment()
                    .setOnDateSetListener(new CalendarDatePickerDialogFragment.OnDateSetListener() {
                        @Override
                        public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
                            Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
                            calendar.setTimeInMillis(0);
                            calendar.set(Calendar.YEAR, year);
                            calendar.set(Calendar.MONTH, monthOfYear);
                            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                            selectedDate = calendar.getTimeInMillis();
                            dateTxt.setText(DateUtils.getTime(selectedDate));
                            dateTxt.setTextColor(Color.WHITE);
                        }
                    })
                    .setFirstDayOfWeek(Calendar.SUNDAY)
                    .setPreselectedDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH))
                    .setDoneText("确定")
                    .setCancelText("取消");
                cdp.show(getSupportFragmentManager(), "FRAG_TAG_DATE_PICKER");
            }
        });
        maleBtn = findViewById(R.id.maleBtn);
        maleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isMale = true;
                maleBtn.setChecked(true);
                femaleBtn.setChecked(false);
            }
        });
        femaleBtn = findViewById(R.id.femaleBtn);
        femaleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isMale = false;
                maleBtn.setChecked(false);
                femaleBtn.setChecked(true);
            }
        });

        departmentSpinner = findViewById(R.id.departmentSpinner);
        departmentSpinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, departments));
        departmentSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                department = departments[i];
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                department = null;
            }
        });
        departmentSpinner.setSelection(0);

        phoneTxt = findViewById(R.id.phoneTxt);

        yesBtn = findViewById(R.id.yesBtn);
        noBtn = findViewById(R.id.noBtn);
        yesBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                yesOrNo = 1;
                yesBtn.setChecked(true);
                noBtn.setChecked(false);
            }
        });
        noBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                yesOrNo = 0;
                yesBtn.setChecked(false);
                noBtn.setChecked(true);
            }
        });

        adviseTxt = findViewById(R.id.adviseTxt);

        findViewById(R.id.quitBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        findViewById(R.id.commitBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = nameTxt.getText().toString();
                if(TextUtils.isEmpty(name)) {
                    sentToast("姓名不能为空");
                    return;
                }
                long selectedDate = MainActivity.this.selectedDate;
                if(selectedDate == -1) {
                    sentToast("请选择出生日期");
                    return;
                }
                boolean isMale = MainActivity.this.isMale;
                String department = MainActivity.this.department;
                if(TextUtils.isEmpty(department)) {
                    sentToast("请选择院系");
                    return;
                }

                String phone = phoneTxt.getText().toString();
                if(TextUtils.isEmpty(name)) {
                    sentToast("手机号码不能为空");
                    return;
                }

                if(!phone.matches("1[0-9]{10}")) {
                    sentToast("请输入正确的手机号");
                    return;
                }

                int yesOrNo = MainActivity.this.yesOrNo;
                if(yesOrNo == -1) {
                    sentToast("请选择是否对食堂满意");
                    return;
                }

                String advise = adviseTxt.getText().toString();
                Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("date", DateUtils.getTime(selectedDate));
                intent.putExtra("sex", isMale ? "男" : "女");
                intent.putExtra("department", department);
                intent.putExtra("phone", phone);
                intent.putExtra("yesOrNo", yesOrNo == 0 ? "不满意" : "满意");
                intent.putExtra("advise", TextUtils.isEmpty(advise) ? "无意见" : advise);
                startActivity(intent);
            }
        });
    }


    private Toast toast;
    private void sentToast(String str) {
        if(toast != null) {
            toast.cancel();
        }
        toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
        toast.show();
    }

}
